package protocol.JT808_2011;

import protocol.JT808_2019.JT808_2019Cmd;

import java.util.HashMap;

public class JT808_2011Utils {

    public static void handle0200ALL(StringBuilder builder, JT808_2011Cmd cmd){

        handle0200Base(cmd, builder);

        if (cmd.leftSize() > 2){
            while (cmd.leftSize() > 2){
                int id = cmd.readInt(1);
                int size = cmd.readInt(1);
                switch (id) {
                    case 0x01:
                        builder.append("里程：" + cmd.readInt(size));
                        builder.append("\r\n");
                        break;
                    case 0x02:
                        builder.append("油量：" + cmd.readInt(size));
                        builder.append("\r\n");
                        break;
                    case 0x03:
                        builder.append("行驶记录仪速度（1/10km/h）：" + cmd.readInt(size));
                        builder.append("\r\n");
                        break;
                    case 0x011: {
                        int type = cmd.readInt(1);
                        HashMap<Integer, String> list = new HashMap<>();
                        list.put(0, "无特定位置");
                        list.put(1, "圆形区域");
                        list.put(2, "矩形区域");
                        list.put(3, "多边形区域");
                        list.put(4, "路段");
                        builder.append("超速报警附加信息：" + type);
                        builder.append("    位置类型(" + type + "):" + list.getOrDefault(type, "未定义"));
                        builder.append("\r\n");
                        if (type != 0){
                            builder.append("    区域或路段ID:" + cmd.readDWORD());
                            builder.append("\r\n");
                        }
                        break;
                    }
                    case 0x12: {
                        int type = cmd.readInt(1);
                        HashMap<Integer, String> list = new HashMap<>();
                        list.put(1, "圆形区域");
                        list.put(2, "矩形区域");
                        list.put(3, "多边形区域");
                        list.put(4, "路段");
                        builder.append("进出区域/路线报警附加信息：" + type);
                        builder.append("    位置类型(" + type + "):" + list.getOrDefault(type, "未定义"));
                        builder.append("\r\n");
                        builder.append("    区域或路段ID:" + cmd.readDWORD());
                        builder.append("\r\n");
                        list.clear();
                        list.put(0, "进");
                        list.put(1, "出");
                        builder.append("    方向:" + list.getOrDefault(cmd.readInt(1), "未定义"));
                        builder.append("\r\n");

                        if (size != 6) {
                            cmd.moveIndex(size - 6);
                        }
                        break;
                    }
                    case 0x13: {
                        builder.append("路段行驶时间不足/过长报警附加信息：");
                        builder.append("\r\n");
                        builder.append("    路段ID:" + cmd.readDWORD());
                        builder.append("\r\n");
                        builder.append("    路段行驶时间:" + cmd.readWORD());
                        builder.append("\r\n");

                        int type = cmd.readInt(1);
                        HashMap<Integer, String> list = new HashMap<>();
                        list.put(0, "不足");
                        list.put(1, "过长");
                        builder.append("    结果(" + type + "):" + list.getOrDefault(cmd.readInt(1), "未定义"));
                        builder.append("\r\n");
                        if (size != 7) {
                            cmd.moveIndex(size - 7);
                        }
                        break;
                    }
                    default:
                        builder.append("未定义ID("+id+")："+cmd.readBCD(size));
                        builder.append("\r\n");
                        break;
                }

            }
        }
    }

    public static void handle0200Base(JT808_2011Cmd cmd, StringBuilder builder){
        int warn = cmd.readInt(4);
        builder.append("\r\n");
        builder.append("--------------基本位置信息-----------------");
        builder.append("\r\n");
        builder.append("报警信息:"+warn);
        builder.append("\r\n");
        builder.append("    紧急报警："+(warn&0x01));
        builder.append("\r\n");
        builder.append("    超速报警："+(warn>>1&0x01));
        builder.append("\r\n");
        builder.append("    疲劳驾驶报警："+(warn>>2&0x01));
        builder.append("\r\n");
        builder.append("    预警："+(warn>>3&0x01));
        builder.append("\r\n");
        builder.append("    GNSS模块发生故障："+(warn>>4&0x01));
        builder.append("\r\n");
        builder.append("    GNSS天线未接或被剪断："+(warn>>5&0x01));
        builder.append("\r\n");
        builder.append("    GNSS天线短路："+(warn>>6&0x01));
        builder.append("\r\n");
        builder.append("    终端主电源欠压："+(warn>>7&0x01));
        builder.append("\r\n");
        builder.append("    终端主电源掉电："+(warn>>8&0x01));
        builder.append("\r\n");
        builder.append("    终端LCD或显示器故障："+(warn>>9&0x01));
        builder.append("\r\n");
        builder.append("    语音合成（TTS）模块故障："+(warn>>10&0x01));
        builder.append("\r\n");
        builder.append("    摄像头故障："+(warn>>11&0x01));
        builder.append("\r\n");

        builder.append("    当天累计驾驶超时报警："+(warn>>18&0x01));
        builder.append("\r\n");
        builder.append("    超时停车报警："+(warn>>19&0x01));
        builder.append("\r\n");
        builder.append("    进出区域报警："+(warn>>20&0x01));
        builder.append("\r\n");
        builder.append("    进出路线报警："+(warn>>21&0x01));
        builder.append("\r\n");
        builder.append("    路段行驶时间不足/过长报警："+(warn>>22&0x01));
        builder.append("\r\n");
        builder.append("    路线偏离报警："+(warn>>23&0x01));
        builder.append("\r\n");
        builder.append("    车辆VSS故障："+(warn>>24&0x01));
        builder.append("\r\n");
        builder.append("    车辆油量异常报警："+(warn>>25&0x01));
        builder.append("\r\n");
        builder.append("    车辆被盗报警："+(warn>>26&0x01));
        builder.append("\r\n");
        builder.append("    车辆非法点火报警："+(warn>>27&0x01));
        builder.append("\r\n");
        builder.append("    车辆非法位移报警："+(warn>>28&0x01));
        builder.append("\r\n");
        builder.append("    碰撞侧翻报警："+(warn>>29&0x01));
        builder.append("\r\n");
        builder.append("\r\n");

        int status = cmd.readInt(4);
        builder.append("状态信息:"+status);
        builder.append("\r\n");
        builder.append("    ACC（1为开）："+(status&0x01));
        builder.append("\r\n");
        builder.append("    定位状态（1位定位）："+(status>>1&0x01));
        builder.append("\r\n");
        builder.append("     南北纬（1为南）："+(status>>2&0x01));
        builder.append("\r\n");
        builder.append("    东西经（1为西）："+(status>>3&0x01));
        builder.append("\r\n");
        builder.append("    停运状态（0为营运,1为停运）:"+(status>>4&0x01));
        builder.append("\r\n");
        builder.append("    经纬度保密插件状态(0未加密 1加密):"+(status>>5&0x01));
        builder.append("\r\n");

        builder.append("    车辆油路(1为断开)："+(status>>10&0x01));
        builder.append("\r\n");
        builder.append("    车辆电路（1为断开）："+(status>>11&0x01));
        builder.append("\r\n");
        builder.append("    车门锁（0为解锁，1为加锁）："+(status>>12&0x01));
        builder.append("\r\n");
        builder.append("\r\n");
        builder.append("纬度："+cmd.readDWORD());
        builder.append("\r\n");
        builder.append("经度："+cmd.readDWORD());
        builder.append("\r\n");
        builder.append("高程："+cmd.readDWORD());
        builder.append("\r\n");
        builder.append("速度(百米/H)"+cmd.readDWORD());
        builder.append("\r\n");
        builder.append("方向（0~359,正北为0，顺时针）："+cmd.readDWORD());
        builder.append("\r\n");
        builder.append("时间："+cmd.readBCD(6));
        builder.append("\r\n");

        builder.append("-------------------------------------------");
        builder.append("\r\n");
    }

}
